﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    /// <summary>
    /// 省台预报员详细成绩报表数据接口
    /// </summary>
    public class HandlerSTIndividualReport : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string forecaster = context.Request.QueryString["forecaster"] != null ? context.Request.QueryString["forecaster"].ToString() : "";
            string begindate = context.Request.QueryString["begindate"] != null ? context.Request.QueryString["begindate"].ToString() : "";
            string enddate = context.Request.QueryString["enddate"] != null ? context.Request.QueryString["enddate"].ToString() : "";
            string sendpoints = context.Request.QueryString["sendpoints"] != null ? context.Request.QueryString["sendpoints"].ToString() : "";
            string semitype = context.Request.QueryString["semitype"] != null ? context.Request.QueryString["semitype"].ToString() : "";
            string tid = context.Request.QueryString["tid"] != null ? context.Request.QueryString["tid"].ToString() : "";

            //获取回调函数名
            string callback = context.Request.QueryString["callback"];
            //json数据
            string json = GetSTIndividualReportData(forecaster, begindate, enddate, sendpoints, semitype, tid);

            context.Response.ContentType = "application/json";
            //输出：回调函数名(json数据)
            context.Response.Write(callback + "(" + json + ")");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 预报员详细成绩报表数据
        /// </summary>
        /// <param name="forecasters">预报员</param>
        /// <param name="begindate"></param>
        /// <param name="enddate"></param>
        /// <param name="sendpoints">发报时刻数组</param>
        /// <param name="semitype"></param>
        /// <param name="tid">程序执行任务号</param>
        /// <returns></returns>

        public string GetSTIndividualReportData(string forecaster, string begindate, string enddate, string sendpoints, string semitype, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            DateTime beginDate = DateTime.Parse(begindate);
            DateTime endDate = DateTime.Parse(enddate);
            ForeSemiType semiType = (ForeSemiType)Enum.Parse(typeof(ForeSemiType), semitype);
            List<ForeSendpointType> sendpointList = new List<ForeSendpointType>();
            foreach (string s in sendpoints.Split(','))
                sendpointList.Add((ForeSendpointType)Enum.Parse(typeof(ForeSendpointType), s.Replace("'", "")));
            ForeSendpointType[] sendpointArray = sendpointList.ToArray();

            STIndividualReport sTIndividualReport = new STIndividualReport(forecaster, connectionString, tid);
            sTIndividualReport.PrepareData(beginDate, endDate, sendpointArray, semiType);
            return sTIndividualReport.GetReportDataString();

        }


    }
}

